#!coding=utf-8
from standard_function import *

def del_appdown_task(driver):
    #清空下载任务
    time.sleep(10)
    try:
        btn=WaitforElementByName(driver,u'管理',30)
    except NoSuchElementException:
        raise  NoSuchElementException
    else:
        btn.click()
    time.sleep(2)
    try:
        btn=WaitforElementByName(driver,u'安装管理',30)
    except NoSuchElementException:
        raise  NoSuchElementException
    else:
        btn.click()
    time.sleep(2)

    while True:
        try:
            tasks=WaitforElementsById(driver,"com.huawei.appmarket:id/app_paused_textview",30)
        except NoSuchElementException:
            break
        else:
            tasks[0].click()
            try:
                clear_btn=WaitforElementByName(driver,u'删除',30)
            except NoSuchElementException:
                continue
            else:
                clear_btn.click()
            time.sleep(2)
    driver.back()
    try:
        btn=WaitforElementByName(driver,u'推荐',30)
    except NoSuchElementException:
        raise  NoSuchElementException
    else:
        btn.click()
    #driver.find_element_by_name("分类").click()

def clickappdown(driver):
    try:
        btns=WaitforElementsById(driver,"com.huawei.appmarket:id/AppListItem",30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        for btn_item in btns:
            btn_item.click()
            try:
                size=WaitforElementById(driver,"com.huawei.appmarket:id/hwdownload_percentage",30).get_attribute("text")
            except NoSuchElementException:
                continue
            print size.split('(')[1][0:-4]
            if 'G' not in size and ',' not in size:
                if 'K' in size or float(size.split('(')[1][0:-4])<20:
                    driver.back()
                    continue
            try:
                btn=WaitforElementById(driver,"com.huawei.appmarket:id/download_framelayout",30)
            except NoSuchElementException:
                driver.back()
                continue
            else:
                btn.click()
            try:
                yes_btn=WaitforElementById(driver,'android:id/button1',30)
            except NoSuchElementException:
                driver.back()
                continue
            else:
                yes_btn.click()
                waittime=20
                while waittime:
                    desc=WaitforElementById(driver,"com.huawei.appmarket:id/hwdownload_percentage",30).get_attribute("text")
                    print desc
                    if '0%' not in desc:
                        btn.click()
                        break
                    else:
                        waittime=waittime-1
                        time.sleep(0.5)
                        if waittime==0:
                            btn.click()
                            break
                print "---------------------------------------------"
                driver.back()

def game(driver):
    gamecatetory = ['动作射击','角色扮演','体育竞速','经营策略','棋牌桌游','休闲益智']
    try:
        ss=WaitforElementByName(driver,u'游戏',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        ss.click()
    time.sleep(5)
    a2=random.randint(0,8)
    print a2
    swipeLift(driver,0.9,a2*0.1)
    time.sleep(2)

    # try:
    #     game_btn=WaitforElementByName(driver,u'游戏',30)
    # except NoSuchElementException:
    #     raise NoSuchElementException
    # else:
    #     game_btn.click()

    for i in gamecatetory:
        try:
            btn=WaitforElementByName(driver,i,30)
        except NoSuchElementException:
            continue
        else:
            btn.click()
        try:
            kk=WaitforElementsById(driver,"com.huawei.appmarket:id/more_btn",30)
        except NoSuchElementException:
            driver.back()
            continue
        else:
            kk[0].click()
            time.sleep(2)
            try:
                clickappdown(driver)
            except NoSuchElementException:
                driver.back()
                continue
            driver.back()
        #driver.back()

def soft(driver):
    gamecatetory= [u'影音娱乐',u'实用工具',u'社交通讯',u'教育',u'新闻阅读',u'拍摄美化',u'美食',u'出行导航',u'旅游住宿',u'购物比价',u'商务',u'儿童', \
                   u'金融理财',u'运动健康',u'便捷生活',u'汽车',u'个性主题',u'AI应用',u'手表应用']
    try:
        btn=WaitforElementByName(driver,u'应用',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        btn.click()
    time.sleep(5)
    a2=random.randint(0,8)
    print a2
    swipeLift(driver,0.9,a2*0.1)
    time.sleep(2)
    for k in gamecatetory:
        print "Current page is:",k

        # try:
        #     btn=WaitforElementByName(driver,u'分类',30)
        # except NoSuchElementException:
        #     raise NoSuchElementException
        # else:
        #     btn.click()

        try:
            btn=WaitforElementByName(driver,k,5)
        except NoSuchElementException:
            continue
        else:
            btn.click()
        try:
            kk=WaitforElementsById(driver,"com.huawei.appmarket:id/more_btn",30)
        except NoSuchElementException:
            driver.back()
            continue
        else:
            kk[0].click()
            time.sleep(2)
            try:
                clickappdown(driver)
            except NoSuchElementException:
                driver.back()
                continue
            driver.back()
        #driver.back()

def run():
    desired_caps = {}
    #desired_caps['automationName'] = 'uiautomator2'
    desired_caps['platformName'] = "Android"               # 声明是ios还是Android系统
    desired_caps['platformVersion'] = ANDROID_VERSION             # Android内核版本号，也可以在模拟器设置中查看
    desired_caps['deviceName'] = DEVICE         # 连接的设备名称，可通过adb devices查看
    desired_caps['appPackage'] = 'com.huawei.appmarket'    # apk的包名
    desired_caps['appActivity'] = '.MainActivity'          # apk的launcherActivity
    desired_caps['noReset']='true'                         #不需要再次安装apk
    driver= webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)          # 建立 session
    #driver.implicitly_wait(30)
    time.sleep(2)
    del_appdown_task(driver)
    soft(driver)
    driver.back()
    game(driver)
    driver.quit()

#run()